package main;

import java.awt.*;
import java.util.ArrayList;

import static main.Game.k;
import static main.Game.random;
import static main.Register.*;

public class LivingEntity extends AbstractEntity {

    public byte health = 20;
    public Image stepLeft;
    public Image stepRight;
    public Image left;
    public Image right;
    public Image stay;
    public boolean flagToWalk;
    public boolean flagToPlayer;
    public boolean flagToZombie;
    public boolean choose = true;
    public double boxWidth = 32;
    public double boxWidthPlus = 0;
    public double boxHeight = 32;
    public double boxHeightPlus = 0;
    public byte COOLDOWN = 0;


    public LivingEntity(Image left,Image stepLeft, Image right, Image stepRight, Image stay, double x, double y) {
        super(stay);


        this.stepLeft = stepLeft;
        this.stepRight = stepRight;
        this.left = left;
        this.right = right;
        this.stay = stay;
        this.posX = x;
        this.posY = y;

        return;
    }
    @Override
    public void update() {
        boxX1 = posX - boxWidth / 2 + boxWidthPlus;
        boxY1 = posY - boxHeight / 2 + boxHeightPlus;
        boxX2 = posX + boxWidth / 2 + boxWidthPlus;
        boxY2 = posY + boxHeight / 2 + boxHeightPlus;

        COOLDOWN = (byte) (COOLDOWN > 0 ? COOLDOWN - 1 : 0);


        if (health <= 0) {
            health = 20;
            this.setPos(0,0);
        }
    }



    public void goTo (double x, double y) {
        double stepx = Double.compare(x, posX);
        double stepy = Double.compare(y, posY);







        if(moveIsTrue(stepx, stepy)) {
            turn = stepx == 0 ? Integer.compare((int) stepy, 0) : Integer.compare((int) stepx, 0);
            posX += stepx;
            posY += stepy;
        }


    }
    public void walk (boolean setFlagToWalk) {

        if (setFlagToWalk) {
            flagToWalk = true;
            choose = false;
        }
        if (flagToWalk) {

            if (toPosX - posX < 0.1 && toPosY - posY < 0.1) {
                flagToWalk = false;
                choose = true;
            }
            goTo(toPosX, toPosY);

        } else if (choose){
            turn = 0;
            toPosX = Game.randInt(64, 256) * (random.nextBoolean() ? 1 : -1) + posX;
            toPosY = Game.randInt(64, 256) * (random.nextBoolean() ? 1 : -1) + posY;
            choose = false;
        }
    }

    public boolean moveIsTrue (double stepx, double stepy) {
        boolean isIn = true;
        for (ArrayList<LivingEntity> list : ENTITIES) {
            for (LivingEntity entity : list) {
                if (entity != this) {
                    if (inTo(stepx, stepy, entity)) {
                        continue;
                    }
                    isIn = false;
                    break;
                }
            }
            if (!isIn) {
                break;
            }
        }
        if (isIn) {
            for (AbstractEntity entity : WALLS) {
                if (inTo(stepx,stepy,entity)) {
                    continue;
                }
                isIn = false;
                break;
            }
        }
        if (isIn) {
            for (LivingEntity entity : PLAYERS) {
                if (entity != this) {
                    if (inTo(stepx,stepy,entity)
                    ) {
                        continue;
                    }
                    isIn = false;
                    break;
                }
            }
        }
        return isIn;
    }
    public double preyPosX ( LivingEntity entity ) {
        return Math.min(Math.abs(this.posX - entity.posX), Math.abs(this.posX - entity.posX));
    }
    public double preyPosY ( LivingEntity entity ) {
        return Math.min(Math.abs(this.posY - entity.posY), Math.abs(this.posY - entity.posY));
    }

    public boolean locateIn (int range, LivingEntity entity) {
        return -range <= preyPosX(entity) && preyPosX(entity) <= range
                && -range <= preyPosY(entity) && preyPosY(entity) <= range;
    }

    public void drawPlayer (Graphics g, Image image) {
        g.drawImage(image,
                (int) ((Game.WIDTH - this.width*k) / 2),
                (int) ((Game.HEIGHT - this.height*k) / 2),
                (int) ((Game.WIDTH + this.width*k) / 2),
                (int) ((Game.HEIGHT + this.height*k) / 2),
                0,0, image.getWidth(null), image.getHeight(null),
                null);
    }

    private boolean inTo (double stepx, double stepy, AbstractEntity entity) {
        return (this.boxX2 + stepx - 1 < entity.boxX1)
                || (this.boxX1 + stepx + 1 > entity.boxX2)
                || (this.boxY2 + stepy - 1 < entity.boxY1)
                || (this.boxY1 + stepy + 1 > entity.boxY2);
    }

    public void drawRideOn (Graphics g, Image image, Player player, int wegthPlus, int heightPlus) {
        g.drawImage(image,
                (int) (Game.WIDTH / 2 - player.playerRideOn.width / 2 * k + wegthPlus),
                (int) (Game.HEIGHT / 2 - player.playerRideOn.height / 2 * k + player.playerRideOn.height / 2 + heightPlus),
                (int) (Game.WIDTH / 2 + player.playerRideOn.width / 2 * k + wegthPlus),
                (int) (Game.HEIGHT / 2 + player.playerRideOn.height / 2 * k + player.playerRideOn.height / 2 + heightPlus),
                0,0, image.getWidth(null),
                image.getHeight(null),
                null);
    }



}
